package org.example;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.spi.CachingProvider;
import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 *
 */
public class Ehcache3Jcache
{
    public static void main( String[] args )
    {
        // 获取 CachingProvider
        CachingProvider provider =
                Caching.getCachingProvider();

        // 获取 CacheManager
        CacheManager cacheManager =
                provider.getCacheManager();

        // 配置缓存
        MutableConfiguration<Object, Object> config =
                new MutableConfiguration<Object, Object>()
                        .setTypes(Object.class, Object.class)
                        .setStoreByValue(false)
                        .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(
                                new Duration(TimeUnit.MINUTES, 5)))
                        .setStatisticsEnabled(true);

        // 创建缓存
        Cache<Object, Object> cache = cacheManager.createCache("jcache-user", config);

        // 使用
        cache.put(1L, "Charlie");
        cache.put(2L, "Charlie11");
        cache.put(3L, "Charlie1122");
        cache.put(4L, "Charlie112233");


        // 缓存统计信息，在ehcache3下，已不再支持获取size，
        System.out.println( "Hello World!"+cache.get(1L) );
    }
}
